<template>
  <div class="row">
    <!--展示用户列表-->
    <div
      v-show="users.length"
      class="card"
      v-for="user in users"
      :key="user.id"
    >
      <a :href="user.organizations_url" target="_blank">
        <img :src="user.avatar_url" style="width: 100px" alt="" />
        <p class="card-text">{{ user.login }}</p>
      </a>
    </div>
    <!--欢迎-->
    <h1 v-if="isFirst">欢迎使用！</h1>
    <!--loading-->
    <h1 v-show="isLoading">加载中...</h1>
    <!--错误信息-->
    <h1 v-show="errorMessage">{{ errorMessage }}</h1>
  </div>
</template>

<script>
export default {
  name: "ListComponent",
  data() {
    return {
      isFirst: true,
      isLoading: false,
      errorMessage: "",
      users: [],
    };
  },
  mounted() {
    this.$bus.$on("refreshList", this.refreshList);
    this.$bus.$on("errorList", (msg)=>{
        this.errorMessage = msg;
    });
    this.$bus.$on("startSearch", ()=>{
        this.isLoading = true;
        this.isFirst = false;
    });
  },
  beforeDestroy() {
    this.$bus.$off("refreshList");
    this.$bus.$off("errorList");
    this.$bus.$off("startSearch");
  },
  methods: {
    refreshList(items) {
      this.isLoading = false;
      this.errorMessage = '';
      this.users = items;
      console.log("list-vue", this.users);
      //es6 beancopy
      //this.targetObj = {...this.targetObj,...dataObje};
    },
  },
};
</script>

<style scoped>
.album {
  min-height: 50rem;
  padding-top: 3rem;
  padding-bottom: 3rem;
  background-color: #f7f7f7;
}
.card {
  float: left;
  width: 33.333%;
  padding: 0.75rem;
  margin-bottom: 2rem;
  border: 1px solid #efefef;
  text-align: center;
}
.card > img {
  margin-bottom: 0.75rem;
  border-radius: 100px;
}
.card-text {
  font-size: 85%;
}
</style>